GBML manual / Drawing

screen_width

How many pixels wide the screen is - ask for it instead of typing a number, so a game keeps working on a screen of a different size.

Syntax

screen_width() -> int

Returns

int

Description

Gives back how many pixels wide the screen is. A pixel is one tiny dot of the picture, and the screen is a grid of them, so this is how many dots fit side to side.

You could type that number yourself, but there is a reason to ask for it instead. Every console has its own screen size, and a game that has the number typed into it only works on the one screen it was written for. A game that asks "how wide is the screen?" gets the right answer wherever it runs, and you never have to hunt through your code for the numbers that were really a screen size in disguise. The number 236 on its own says nothing. screen_width() - 4 says "4 pixels in from the edge", and it says it on any screen.

The screen is not the same thing as the room. The room is the whole level, and it can be much bigger than what fits on screen at once. room_width is the level's width, and screen_width is the width of the window you look at it through.

Which way to put something on the screen?

There are three, and you can use them side by side. Try them in this order:

  1. Measuring from the top-left corner, which is what a plain draw call in a Draw GUI event does ((0, 0) is the top-left pixel), needs no screen size at all, so use it whenever the spot is "a bit in from the top-left".
  2. An anchor names one of nine spots, such as the bottom-right corner, and moves from there. Use it when you can describe the spot in words: "the corner", "the middle of the bottom".
  3. screen_width and screen_height are for when you have to do the sum yourself, such as centring something of a size you know, or spacing a row of things evenly across the screen.

Example

// four labels, evenly spaced along the top of the screen
var gap = screen_width() / 4;
for (var i = 0; i < 4; i++) {
    draw_text_anchored(ANCHOR_TOP_LEFT, gap * i + gap / 2, 8, "*");
}

Cuts the screen's width into four equal slices, then draws a star in the middle of each one, 8 pixels down from the top. Nothing in it names the size of the screen, so it lines up the same on any screen.

On the whole numbers

screen_width() is a whole number, and screen_width() / 2 throws away a leftover half pixel if the width is odd, the same as every whole-number division in GBML. Screens are almost always an even number of pixels wide, so the halves come out exact.

On the GBA

The GBA screen is 240 pixels wide, so this is 240. It is a fixed number known when the game is built, so it costs nothing at run time: writing screen_width() is exactly like writing 240 in your code, with none of the risk that the 240 is out of date.

See also: screen_heightdraw_text_anchoredroom_widthScreen anchors